import vehicle.Car;
import vehicle.Bus;
import vehicle.Truck;
import vehicle.Vehicle;
import user.Administrator;
import user.User;
import user.VIPUser;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class VehicleRentalSystem {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "1234";

    public static void main(String[] args) {
        List<Vehicle> vehicleList = new ArrayList<>();
        // 预先存入车辆信息
//        vehicleList.add(new Car("川R123", "华为"));
//        vehicleList.add(new Car("川R456", "小米"));
//        vehicleList.add(new Truck("川Rabc", "大众"));
//        vehicleList.add(new Truck("川Rdef", "广汽"));
//        vehicleList.add(new Bus("川R111", "比亚迪"));
//        vehicleList.add(new Bus("川R222", "特斯拉"));

        Scanner scanner = new Scanner(System.in);

        // 加载数据库驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        User user;
        if (username.equals("admin") && password.equals("1234")) {
            user = new Administrator(username, password);
        } else {
            user = new VIPUser(username, password);
        }

        if (user.login(username, password)) {
            user.operate(vehicleList);
            // 将用户信息插入到 users 表
            insertUserIntoDB(user);
            // 将车辆信息插入到 vehicles 表
            insertVehiclesIntoDB(vehicleList);
        } else {
            System.out.println("登录失败，请重新输入。");
        }

        scanner.close();
    }


    private static void insertUserIntoDB(User user) {
        String userType = user instanceof Administrator? "Administrator" : "VIPUser";
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement statement = connection.prepareStatement("INSERT INTO users (username, password, user_type) VALUES (?,?,?)")) {
            statement.setString(1, user.username);
            statement.setString(2, user.password);
            statement.setString(3, userType);
            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("用户信息插入成功。");
            } else {
                System.out.println("用户信息插入失败。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void insertVehiclesIntoDB(List<Vehicle> vehicleList) {
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            for (Vehicle vehicle : vehicleList) {
                String vehicleType = null;
                if (vehicle instanceof Car) {
                    vehicleType = "Car";
                } else if (vehicle instanceof Truck) {
                    vehicleType = "Truck";
                } else if (vehicle instanceof Bus) {
                    vehicleType = "Bus";
                }
                try (PreparedStatement statement = connection.prepareStatement("INSERT INTO vehicles (车牌号, 车辆品牌, 车辆类型, 租赁情况, 租赁天数) VALUES (?,?,?,?,?)")) {
                    statement.setString(1, vehicle.getLicensePlate());
                    statement.setString(2, vehicle.getBrand());
                    statement.setString(3, vehicleType);
                    statement.setBoolean(4, vehicle.isRented());
                    statement.setInt(5, vehicle.getRentDays());
                    int rowsInserted = statement.executeUpdate();
                    if (rowsInserted > 0) {
                        System.out.println("车辆信息插入成功：" + vehicle.getLicensePlate());
                    } else {
                        System.out.println("车辆信息插入失败：" + vehicle.getLicensePlate());
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}